package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.vo.OrderSubmitParam;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 预约控制器
 */
@RestController
@RequestMapping("/mobile/order")
@Slf4j
@Api(tags = "手机端订单接口")
public class MobileOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderSettingService orderSettingService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private RedisTemplate redisTemplate;

    /* 预约提交 */
    @PostMapping("/submit")
    public Result orderSubmit(@RequestBody Map map, HttpSession session) {
        String telephone = map.get("telephone").toString();
        String code = map.get("validateCode").toString();
        String trueCode = (String) redisTemplate.opsForValue().get(telephone);
        if (code.length() == 0) {
            return new Result(false, "验证码不得为空!");
        } else if (!code.equals(trueCode)) {
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }
        String orderDate = map.get("orderDate").toString();
        if (orderDate == null || orderDate.length() == 0) {
            return new Result(false, "预约日期不能为空!");
        }
        /* 判断当日是否可预约 */
        Date date;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(orderDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        if (orderSettingService.findByDate(date) == null) {
            return new Result(false, MessageConst.SELECTED_DATE_CANNOT_ORDER);
        } else if (orderSettingService.findRereservationsByDate(date) <= 0) {
            return new Result(false, MessageConst.ORDER_FULL);
        }
        /* 判断该号码用户是否是会员，是则更新，不是则创建添加 */
        Member member = memberService.findByTelephone(telephone);
        if (member == null) {
            member = new Member();
            member.setName(map.get("name").toString());
            member.setSex(map.get("sex").toString());
            member.setIdCard(map.get("idCard").toString());
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            memberService.save(member);
        } else {
            member.setName(map.get("name").toString());
            member.setSex(map.get("sex").toString());
            member.setIdCard(map.get("idCard").toString());
            memberService.updateMember(member);
        }
        /* 判断该用户该日是否预约过此套餐 */
        Integer setmealId = Integer.parseInt(map.get("setmealId").toString());
        if (orderService.findSetmealByDate(member.getId(), date, setmealId) != null) {
            return new Result(false, MessageConst.HAS_ORDERED);
        }
        /* 满足以上条件,可添加订单 */
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(date);
        order.setOrderType("微信预约");
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(setmealId);
        orderService.addOrder(order);
        return new Result(true, MessageConst.ORDER_SUCCESS, order);
    }

    @GetMapping("/findById")
    public Result findById(Integer id) {
        Map<String, Object> data = orderService.findById(id);
        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS, data);
    }
}
